<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'material:nature:pmProjectNature:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template> -->
      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.createDate }}
        </a>
      </template> -->
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMaterialNaturePmProjectNatureList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent,watch, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmProjectNatureDelete, pmProjectNatureListData } from '/@/api/material/nature/pmProjectNature';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('material.nature.pmProjectNature');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('水表清单'),
  };

  const props = defineProps({
    projectno: { type: String, default: '' },
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
    {
      label: t('项目编号'),
      field: 'projectno',
      component: 'Input',
      show: false,
    },
    {
      label: t('水表编号'),
      field: 'metercode',
      component: 'Input',
    },
    {
      label: t('水表名称'),
      field: 'metername',
      component: 'Input',
    },
    ],
  };

  const tableColumns: BasicColumn[] = [
   // {
  //   title: t('项目编号'),
  //   dataIndex: 'projectno',
  //   key: 'a.projectno',
  //   sorter: true,
  //   width: 130,
  //   align: 'left',
  // },
  {
    title: t('水表编号'),
    dataIndex: 'metercode',
    key: 'a.metercode',
    sorter: true,
    width: 100,
    align: 'left',
  },
  {
    title: t('水表名称'),
    dataIndex: 'metername',
    key: 'a.metername',
    sorter: true,
    width: 120,
    align: 'left',
  },
  {
    title: t('数量'),
    dataIndex: 'metercount',
    key: 'a.metercount',
    sorter: true,
    width: 80,
    align: 'center',
  },
  {
    title: t('剩余数'),
    dataIndex: 'remaining',
    key: 'a.remaining',
    sorter: true,
    width: 80,
    align: 'center',
  },
  {
    title: t('性质编码'),
    dataIndex: 'naturecode',
    key: 'a.naturecode',
    sorter: true,
    width: 100,
    align: 'left',
  },
  {
    title: t('性质名称'),
    dataIndex: 'naturename',
    key: 'a.naturename',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('口径'),
    dataIndex: 'caliber',
    key: 'a.caliber',
    sorter: true,
    width: 100,
    align: 'left',
  },
  // {
  //   title: t('加压等级编号'),
  //   dataIndex: 'pressuregradecode',
  //   key: 'a.pressuregradecode',
  //   sorter: true,
  //   width: 130,
  //   align: 'left',
  // },
  {
    title: t('加压等级'),
    dataIndex: 'pressuregradename',
    key: 'a.pressuregradename',
    sorter: true,
    width: 100,
    align: 'left',
  },
  {
    title: t('DM分区'),
    dataIndex: 'dmapartition',
    key: 'a.dmapartition',
    sorter: true,
    width: 100,
    align: 'left',
  },
  {
    title: t('监测表'),
    dataIndex: 'ismonitor',
    key: 'a.ismonitor',
    sorter: true,
    width: 80,
    align: 'center',
    dictType: 'sys_yes_no',
  },
  {
    title: t('消防表'),
    dataIndex: 'isfirecontrol',
    key: 'a.isfirecontrol',
    sorter: true,
    width: 80,
    align: 'center',
    dictType: 'sys_yes_no',
  },
  // {
  //   title: t('是否设计变更'),
  //   dataIndex: 'isdesignerchange',
  //   key: 'a.isdesignerchange',
  //   sorter: true,
  //   width: 130,
  //   align: 'center',
  //   dictType: 'sys_yes_no',
  // },
  // {
  //   title: t('是否分单'),
  //   dataIndex: 'issplitorder',
  //   key: 'a.issplitorder',
  //   sorter: true,
  //   width: 130,
  //   align: 'center',
  //   dictType: 'sys_yes_no',
  // },
  // {
  //   title: t('分单编号'),
  //   dataIndex: 'splitprojectno',
  //   key: 'a.splitprojectno',
  //   sorter: true,
  //   width: 130,
  //   align: 'left',
  // },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑水表'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'material:nature:pmProjectNature:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除水表'),
        popConfirm: {
          title: t('是否确认删除水表'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'material:nature:pmProjectNature:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: pmProjectNatureListData,
    beforeFetch: (params) => {
      params.projectno = props.projectno
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmProjectNatureDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  function handleSuccess() {
    reload();
  }

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    reload();
    // await getForm().setFieldsValue({
    //   projectno: props.projectno?props.projectno:userStore.getProjecte.code,
    // });
    // reload();
    // await getForm().setFieldsValue({
    //   projectno: props.projectno?props.projectno:userStore.getProjecte.code,
    // });
  }
</script>
